package com.yang.dev.modular.user.listener;

import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.yang.dev.modular.log.util.DevLogUtil;
import com.yang.dev.modular.user.entity.SysUser;
import com.yang.dev.modular.user.service.SysUserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 自定义登录监听器
 *
 * @author: yangjianzhi
 * @version1.0
 */
@Component
public class AuthListener implements SaTokenListener {

    @Resource
    private SysUserService sysUserService;

    /** 每次登录时触发 */
    @Override
    public void doLogin(String s, Object o, String s1, SaLoginModel saLoginModel) {
        // 记录B端登录日志
        Object name = saLoginModel.getExtra("name");
        if(ObjectUtil.isNotEmpty(name)) {
            DevLogUtil.executeLoginLog(Convert.toStr(name));
        } else {
            DevLogUtil.executeLoginLog(null);
        }
    }

    /** 每次注销时触发 */
    @Override
    public void doLogout(String s, Object o, String s1) {
        // 记录B端登出日志
        SysUser sysUser = sysUserService.getById(Convert.toStr(o));
        if(ObjectUtil.isNotEmpty(sysUser)) {
            DevLogUtil.executeLogoutLog(sysUser.getName());
        } else {
            DevLogUtil.executeLogoutLog(null);
        }
    }

    @Override
    public void doKickout(String s, Object o, String s1) {

    }

    @Override
    public void doReplaced(String s, Object o, String s1) {

    }

    @Override
    public void doDisable(String s, Object o, String s1, int i, long l) {

    }

    @Override
    public void doUntieDisable(String s, Object o, String s1) {

    }

    @Override
    public void doCreateSession(String s) {

    }

    @Override
    public void doLogoutSession(String s) {

    }

    @Override
    public void doRenewTimeout(String s, Object o, long l) {

    }
}
